package com.example.time_planner.dataBase;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TaskHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "Tasks Database";

	private static class Tasks {
		private static final String TABLE_NAME = "Tasks";

		private static final String COLUMN_ID = "_id";
		private static final String COLUMN_TASK_NAME = "TaskName";
		private static final String COLUMN_DESCRIPTION = "Description";
		private static final String COLUMN_CATEGORY = "Category";
		private static final String COLUMN_DATE = "Date";
		private static final String COLUMN_DURATION = "Duration";
		private static final String COLUMN_ONTOP = "IsOnTop";
	}

	private static final String CREATE_STATEMENT = "CREATE TABLE "
			+ Tasks.TABLE_NAME + "(" + Tasks.COLUMN_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Tasks.COLUMN_TASK_NAME
			+ " TEXT, " + Tasks.COLUMN_DESCRIPTION + " TEXT, "
			+ Tasks.COLUMN_CATEGORY + " TEXT, " + Tasks.COLUMN_DATE + " TEXT, "
			+ Tasks.COLUMN_ONTOP + "  TEXT, " + Tasks.COLUMN_DURATION
			+ " TEXT) ";

	private static final String DELETE_STATEMENT = "DELETE IF EXISTS TABLE "
			+ Tasks.COLUMN_TASK_NAME;

	public TaskHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_STATEMENT);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(DELETE_STATEMENT);
		onCreate(db);
	}

	public boolean addTask(Task task) {
		final SQLiteDatabase db = getWritableDatabase();
		String[] checkData = new String[5];
		checkData[0] = task.getTaskName();
		checkData[1] = task.getDescription();
		checkData[2] = task.getCategory();
		checkData[3] = task.getDate();
		checkData[4] = task.getTime();
		if (checkInfo(checkData)) {
			final ContentValues values = new ContentValues();
			values.put(Tasks.COLUMN_TASK_NAME, task.getTaskName());
			values.put(Tasks.COLUMN_DESCRIPTION, task.getDescription());
			values.put(Tasks.COLUMN_CATEGORY, task.getCategory());
			values.put(Tasks.COLUMN_DATE, task.getDate());
			values.put(Tasks.COLUMN_DURATION, task.getTime());
			values.put(Tasks.COLUMN_ONTOP, task.isOnTop());
			db.insert(Tasks.TABLE_NAME, null, values);

			db.close();

			return true;
		}
		return false;
	}

	public List<String> getCategories() {
		return null;

	}


	public List<String> getNames() {
        String selectQuery = "SELECT  " + Tasks.COLUMN_TASK_NAME + " FROM " + Tasks.TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
                 List<String> data = new ArrayList<String>();
        if (cursor.moveToFirst()) {
            do {
               // get  the  data into array,or class variable
            	data.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
        db.close();
        return data;
	}
	
	private boolean checkInfo(String[] string) {
		boolean flag = false;
		for (String str : string) {
			if (str != "") {
				flag = true;
			}
			if (flag == false) {
				return false;
			}
		}
		return flag;
	}
}